﻿using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ProTrack.Report
{
   public class WorkBookHelper
    {
        /// <summary>
        /// 创建单元格样式
        /// </summary>
        /// <param name="_wokebook">工作簿对象</param>
        /// <param name="horizontal">水平对齐方式</param>
        /// <param name="vertical">垂直对齐方式</param>
        /// <param name="IsAddBorder">是否添加边框</param>
        /// <param name="FontSize">字体大小</param>
        /// <param name="IsBold">是否加粗字体</param>
        /// <returns></returns>
        public static HSSFCellStyle CreateCellStyle(IWorkbook _wokebook, HorizontalAlignment horizontal, VerticalAlignment vertical, bool IsAddBorder, int FontSize, bool IsBold,bool WrapText=false)
        {
            var cellStyle = (HSSFCellStyle)_wokebook.CreateCellStyle();

            cellStyle.Alignment = horizontal; //水平居中
            cellStyle.VerticalAlignment = vertical; //垂直居中

            if (IsAddBorder)
            {
                //增加边框
                cellStyle.BorderBottom = BorderStyle.Thin;
                cellStyle.BorderRight = BorderStyle.Thin;
                cellStyle.BorderTop = BorderStyle.Thin;
                cellStyle.BorderLeft = BorderStyle.Thin;
            }
            var cellStyleFont = (HSSFFont)_wokebook.CreateFont(); //创建字体
            cellStyleFont.FontHeightInPoints = FontSize;
            cellStyleFont.IsBold = IsBold;

            cellStyle.SetFont(cellStyleFont);

            cellStyle.WrapText = WrapText;

            return cellStyle;
        }



        public static HSSFCellStyle CreateCellStyle(IWorkbook _wokebook, HorizontalAlignment horizontal, VerticalAlignment vertical, bool IsAddBorderBottom, bool IsAddBorderRight, bool IsAddBorderTop, bool IsAddBorderLeft, int FontSize, bool IsBold, bool WrapText = false)
        {
            var cellStyle = (HSSFCellStyle)_wokebook.CreateCellStyle();

            cellStyle.Alignment = horizontal; //水平居中
            cellStyle.VerticalAlignment = vertical; //垂直居中

            //增加边框
            if (IsAddBorderBottom)
                cellStyle.BorderBottom = BorderStyle.Thin;
            if (IsAddBorderRight)
                cellStyle.BorderRight = BorderStyle.Thin;
            if (IsAddBorderTop)
                cellStyle.BorderTop = BorderStyle.Thin;
            if (IsAddBorderLeft)
                cellStyle.BorderLeft = BorderStyle.Thin;


            var cellStyleFont = (HSSFFont)_wokebook.CreateFont(); //创建字体
            cellStyleFont.FontHeightInPoints = FontSize;
            cellStyleFont.IsBold = IsBold;

            cellStyle.SetFont(cellStyleFont);

            cellStyle.WrapText = WrapText;

            return cellStyle;
        }

        public static byte[] ToExcelByteArray(IWorkbook _workbook)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                _workbook.Write(ms,false);
                byte[] bt = ms.ToArray();
                ms.Write(bt, 0, (int)ms.Length);
                return bt;
            }
        }
    }
}
